﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HydroDesktop.Interfaces.ObjectModel;
using System.IO;

namespace DroughtAnalysis
{
    /// <summary>
    /// Helper methods for station name
    /// </summary>
    public static class StationNameHelper
    {
        /// <summary>
        /// Generates a correctly-formatted (without diacriticm) station name
        /// </summary>
        /// <param name="stationName">the non-diacritic station name</param>
        /// <returns></returns>
        public static string GetStationFileName(string stationName)
        {
            stationName = stationName.Replace(" - ", "-");
            stationName = stationName.Replace("/", "");
            stationName = stationName.Replace("\\", "");
            stationName = stationName.Replace(" ", "_");
            return RemoveDiacritism(stationName);
        }
        
        /// <summary>
        /// Removes any diacritic characters and replaces them by ascii characters (important for the file name)
        /// </summary>
        /// <param name="Text">the text in unicode characters</param>
        /// <returns>the converted text in ascii characters</returns>
        public static string RemoveDiacritism(string Text)
        {
            string stringFormD = Text.Normalize(System.Text.NormalizationForm.FormD);
            System.Text.StringBuilder retVal = new System.Text.StringBuilder();
            for (int index = 0; index < stringFormD.Length; index++)
            {
                if (System.Globalization.CharUnicodeInfo.GetUnicodeCategory(stringFormD[index]) != System.Globalization.UnicodeCategory.NonSpacingMark)
                    retVal.Append(stringFormD[index]);
            }
            return retVal.ToString().Normalize(System.Text.NormalizationForm.FormC);
        }

        /// <summary>
        /// Prepares the directory for the station
        /// </summary>
        /// <param name="baseDirectory">the base directory. This is the root directory for creating output.</param>
        /// <param name="station">the station</param>
        /// <returns>the name of the directory</returns>
        public static string PrepareStationDirectory(string baseDirectory, Site station)
        {
            // check inputs..
            if (!Directory.Exists(baseDirectory)) 
                throw new ArgumentException("directory " + baseDirectory + " doesn't exist.");
            
            string stationOutDir = baseDirectory;
            try
            {
                string stationNameEnglish = StationNameHelper.GetStationFileName(station.Name);
                stationOutDir = Path.Combine(baseDirectory, stationNameEnglish);
                if (!Directory.Exists(stationOutDir))
                {
                    Directory.CreateDirectory(stationOutDir);
                }
                else
                {
                    // try to delete any existing files in the output directory
                    foreach (string file in Directory.GetFiles(stationOutDir))
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch { }
                    }
                }
            }
            catch { }
            return stationOutDir;
        }
    }
}
